const app = getApp();
const common = require('../../../utils/common.js');
const api = require('../../../config/api.js');

var pageNo = 1;
var hadLastPage = false;
var ordersStatus = '';
var payStatus = '';
var shippingStatus = '';

var isLoad = 0;
var isOperate = 0;

Page({
  data:{
    imgUrl: app.globalData.imgurl,
    ordercateList:[{
      id:'0',
      name:'全部',
      selected:true
    },{
      id:'1',
      name:'待支付',
      selected:false
    },{
      id:'2',
      name:'待发货',
      selected:false
    },{
      id:'3',
      name:'待收货',
      selected:false
    },{
      id:'4',
      name:' 已完成',
      selected:false
    }],
    orderList: [],
		showNoData: false,
		showLoding: true
  },
  selectTag: function (index) {
    var ordercateList2 = this.data.ordercateList;
    for (var i = 0; i < ordercateList2.length; i++) {
      ordercateList2[i].selected = false;
    }
    ordercateList2[index].selected = true;
    this.setData({
      ordercateList: ordercateList2
    })
    if (index == '1') {
      ordersStatus = '1';
      payStatus = '0';
      shippingStatus = '0';
    } else if (index == '2') {
      ordersStatus = '1';
      payStatus = '1';
      shippingStatus = '0';
    } else if (index == '3') {
      ordersStatus = '1';
      payStatus = '1';
      shippingStatus = '1';
    } else if (index == '4') {
      ordersStatus = '2';
      payStatus = '1';
      shippingStatus = '2';
    } else if (index == '0') {
      ordersStatus = '';
      payStatus = '';
      shippingStatus = '';
    }
  },
  goDetail: function (e) {
		var orderId = e.currentTarget.dataset.orderId;
    wx:wx.navigateTo({
			url: '/pages/ucenter/orderDetail/orderDetail?orderId=' + orderId
    })
  },
  //选择订单状态
  orderStateSelect: function (e) {
    if (isLoad == 1) {
      return;
    }
    const index = e.currentTarget.dataset.xuhao;
    this.selectTag(index);
    pageNo = 1;
    hadLastPage = false;
    this.setData({
      orderList: []
    });
    this.loadList();
  },
  onLoad: function (options) {
    var index = options.id;
    this.selectTag(index);
  },
  onShow: function () {
    pageNo = 1;
    hadLastPage = false;
    this.setData({
      orderList: []
    });
    this.loadList();
  },
  onReachBottom: function () {
    this.loadList();
  },
  //加载列表
  loadList: function() {
		isOperate = 0;
		isLoad = 1;
		if (hadLastPage) {
			isLoad = 0;
			return;
		}
		var that = this;
		var data = {
			pageNo: pageNo,
			ordersStatus: ordersStatus,
			payStatus: payStatus,
			shippingStatus: shippingStatus
		}
		this.setData({
			showLoding: true,
			showNoData: false
		});
		setTimeout(function () {
			common.request(api.OrderList, data, 'POST').then(function (data) {
				that.setData({
					showLoding: false
				});
				if (data.type == 'success') {
					var orderList = that.data.orderList;
					for (var i = 0; i < data.data.orderList.length; i++) {
						orderList.push(data.data.orderList[i]);
					}
					if (data.data.pageNo == data.data.totalPages) {
						hadLastPage = true;
					} else {
						pageNo++;
					}
					that.setData({
						orderList: orderList
					});
					isLoad = 0;
					if (data.data.total == 0) {
						that.setData({
							showNoData: true
						});
					}
				} else {
					console.log(data.content);
					isLoad = 0;
				}
			});
		}, app.globalData.showLodingTime);
  },
  //操作订单
	operateOrder: function (e) {
    if (isOperate == 1) {
      return false;
    }
    isOperate = 1;
		var operateType = e.currentTarget.dataset.operateType;
		var content = '';
		var url = '';
		if (operateType == 'reject') {
			content = '确认申请退款吗';
			url = api.OrderReject;
		} else if (operateType == 'cancel') {
			content = '确定取消订单吗';
			url = api.OrderCancel;
		} else if (operateType == 'pay') {
			content = '确定支付订单吗';
			url = api.OrderPay;
		} else if (operateType == 'confirm') {
			content = '确定收货吗';
			url = api.OrderConfirm;
		} else {
			isOperate = 0;
			return false;
		}
    var that = this;
    wx.showModal({
      title: '提示',
			content: content,
      success: function(res) {
        var data = {
          orderId: e.currentTarget.dataset.orderId
        }
        if (res.confirm) {
					common.request(url, data, 'POST') .then(function (data) {
            if (data.type == 'success') {
							common.showSuccessToast(data.content);
              setTimeout(function () {
                pageNo = 1;
                hadLastPage = false;
                that.setData({
                  orderList: []
                });
                that.loadList();
              }, 1500)              
            } else {
							common.showErrorToast(data.content);
              isOperate = 0;
            }
          });
        } else if (res.cancel) {
          isOperate = 0;
        }
      }
    })
  }
})